package com.neusoft.neuiotms.dcom.converter.insurance;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.insurance.InsuranceRegisterDTO;
import com.neusoft.neuiotms.dcom.dto.insurance.InsuranceRegisterTaskDTO;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceRegister;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceRegisterTask;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceRegisterUnit;
import com.neusoft.neuiotms.dcom.repository.insurance.InsuranceRegisterRepository;
import com.neusoft.neuiotms.dcom.repository.insurance.InsuranceRegisterUnitRepository;

@Component
public class InsuranceRegisterConverter
        extends AbstractConverter<InsuranceRegister, InsuranceRegisterDTO> {

    @Autowired
    private BaseIdGenerator insuranceRegisterCodeGenerator;

    @Autowired
    private InsuranceRegisterRepository insuranceRegisterRepository;

    @Autowired
    private InsuranceRegisterUnitConverter insuranceRegisterUnitConverter;
    @Autowired
    private InsuranceRegisterUnitRepository insuranceRegisterUnitRepository;

    @Autowired
    private InsuranceRegisterTaskConverter registerTaskConverter;

    @Override
    public InsuranceRegister toModel(final InsuranceRegisterDTO dto) {

        InsuranceRegister model;

        if (dto.isNew()) {
            model = new InsuranceRegister();
            if (StringUtils.isEmpty(dto.getCode())) {
                model.setCode(this.insuranceRegisterCodeGenerator.generateCode());
            } else {
                model.setCode(dto.getCode());
            }
        } else {
            model = this.insuranceRegisterRepository.getOne(dto.getId());
        }

        model.setStartTime(dto.getStartTime());
        model.setEndTime(dto.getEndTime());
        model.setRemarks(dto.getRemarks());
        model.setCustomerContact(dto.getCustomerContact());
        model.setCustomerContactPhone(dto.getCustomerContactPhone());
        model.setReleaseStatus(dto.getReleaseStatus());

        // 服务单位
        final List<InsuranceRegisterUnit> items =
                model.getRegisterUnit().stream().collect(Collectors.toList());

        items.forEach(k -> {
            model.getRegisterUnit().remove(k);
            this.insuranceRegisterUnitRepository.delete(k);
        });

        dto.getRegisterUnit().stream().map(s -> {
            final InsuranceRegisterUnit itemModel = this.insuranceRegisterUnitConverter.toModel(s);
            itemModel.setInsuranceRegister(model);
            return itemModel;
        }).forEach(model.getRegisterUnit()::add);

        final List<InsuranceRegisterTask> registerTask =
                model.getRegisterTask().stream().collect(Collectors.toList());
        // 判断是否更新任务表
        if (model.getReleaseStatus() && (registerTask.size() == 0)) {
            //新增
            model.getRegisterUnit().stream().map(s -> {
                final InsuranceRegisterTaskDTO taskDto = new InsuranceRegisterTaskDTO();
                taskDto.setRegisterCode(model.getCode());
                taskDto.setUnitCode(s.getUnitCode());
                taskDto.setUnitName(s.getUnitName());
                taskDto.setStartTime(model.getStartTime());
                taskDto.setEndTime(model.getEndTime());
                taskDto.setRegisterRemarks(model.getRemarks());
                final InsuranceRegisterTask taskModel = this.registerTaskConverter.toModel(taskDto);
                taskModel.setInsuranceRegister(model);
                return taskModel;
            }).forEach(model.getRegisterTask()::add);
        }

        return model;
    }

    @Override
    public InsuranceRegisterDTO toDTO(final InsuranceRegister model, final boolean forListView) {

        final InsuranceRegisterDTO dto = new InsuranceRegisterDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setRemarks(model.getRemarks());
        dto.setCustomerContact(model.getCustomerContact());
        dto.setCustomerContactPhone(model.getCustomerContactPhone());
        dto.setReleaseStatus(model.getReleaseStatus());

        model.getRegisterUnit().stream().map(s -> this.insuranceRegisterUnitConverter.toDTO(s))
                .filter(k -> null != k)
                .forEach(dto.getRegisterUnit()::add);

        return dto;
    }
}
